package aggregate

import "context"

type EventService interface {
	// subscribe 订阅事件流
	subscribe() (result <-chan Event)

	// publish 发布事件
	publish(ctx context.Context, event Event) error
}

// EventKind 事件类型别名
type EventKind string

type Event struct {
	WorkflowID string
	NodeID     string

	ReviewTaskID string
	Kind         EventKind
}

// NewNodePassedEvent 创建节点通过事件
func NewNodePassedEvent(workflowID, nodeID, reviewTaskID string) Event {
	return Event{
		WorkflowID:   workflowID,
		NodeID:       nodeID,
		ReviewTaskID: reviewTaskID,
		Kind:         EventKindNodePassed,
	}
}

// NewNodeRejectedEvent 创建节点拒绝事件
func NewNodeRejectedEvent(workflowID, nodeID, reviewTaskID string) Event {
	return Event{
		WorkflowID:   workflowID,
		NodeID:       nodeID,
		ReviewTaskID: reviewTaskID,
		Kind:         EventKindNodeRejected,
	}
}

// NewReviewTasksCreatedEvent 创建审批任务创建事件
func NewReviewTasksCreatedEvent(workflowID, nextNodeID, reviewTaskID string) Event {
	return Event{
		WorkflowID:   workflowID,
		NodeID:       nextNodeID,
		ReviewTaskID: reviewTaskID,
		Kind:         EventKindReviewTasksCreated,
	}
}

// NewWorkflowPassedEvent 创建工作流通过事件
func NewWorkflowPassedEvent(workflowID, nodeID, reviewTaskID string) Event {
	return Event{
		WorkflowID:   workflowID,
		NodeID:       nodeID,
		ReviewTaskID: reviewTaskID,
		Kind:         EventKindWorkflowPassed,
	}
}

// NewWorkflowRejectedEvent 创建工作流拒绝事件
func NewWorkflowRejectedEvent(workflowID, nodeID, reviewTaskID string) Event {
	return Event{
		WorkflowID:   workflowID,
		NodeID:       nodeID,
		ReviewTaskID: reviewTaskID,
		Kind:         EventKindWorkflowRejected,
	}
}

// 事件类型常量定义，对应原来Hook接口中的方法
// 事件触发顺序：EventKindNodePassed/EventKindNodeRejected -> EventKindReviewTasksCreated -> EventKindWorkflowPassed/EventKindWorkflowRejected
const (
	// EventKindNodePassed 节点审批通过事件
	// 触发时机：当整个节点被审批通过后立即触发（第1步）
	// 包含信息：workflowID, nodeID, reviewTaskID（被通过的审批任务ID）, reviewerID（审批人ID）
	EventKindNodePassed EventKind = "node_passed"

	// EventKindNodeRejected 节点审批拒绝事件
	// 触发时机：当整个节点审批被拒绝后立即触发（第1步）
	// 包含信息：workflowID, nodeID, reviewTaskID（被拒绝的审批任务ID）, reviewerID（审批人ID）
	EventKindNodeRejected EventKind = "node_rejected"

	// EventKindReviewTasksCreated 审批任务创建事件
	// 触发时机：当节点审批结束并流转到下个节点，下个节点的审批任务创建完成后触发（第2步）
	// 包含信息：businessID, formContent, businessCode, nextNodeID, nextNodeReviewTasks
	// 注意：只有在节点通过且存在下个节点时才会触发
	EventKindReviewTasksCreated EventKind = "review_tasks_created"

	// EventKindWorkflowPassed 工作流审批通过事件
	// 触发时机：当整个工作流审批完成并通过后触发（第3步，最后一步）
	// 包含信息：businessID, formContent, businessCode, nodeReviewTasks（所有节点的审批任务）
	EventKindWorkflowPassed EventKind = "workflow_passed"

	// EventKindWorkflowRejected 工作流驳回事件
	// 触发时机：当工作流被驳回时触发（可能在任意节点被拒绝时发生）
	// 包含信息：workflowID, businessID, formContent, businessCode
	EventKindWorkflowRejected EventKind = "workflow_rejected"
)
